package sz.szsmk.citizencard.ui.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.OpenClientUtil;
import com.baidu.mapapi.utils.route.BaiduMapRoutePlan;
import com.baidu.mapapi.utils.route.RouteParaOption;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusTransViewBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.mvp.contract.IBusHomeTransContact;
import sz.szsmk.citizencard.mvp.presenter.BusHomeTransPresenter;
import sz.szsmk.citizencard.receiver.LocationReceiver;
import sz.szsmk.citizencard.ui.activity.BusHomeActivity;
import sz.szsmk.citizencard.ui.activity.IMainActivity;
import sz.szsmk.citizencard.ui.adapter.BusTransAdapter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.MesageEventBus;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/28 15:10
 * description:
 */
@SuppressLint("ValidFragment")
public class BusHomeTransFragment extends IBaseFragment<BusHomeTransPresenter, BusTransViewBinding>
        implements IBusHomeTransContact.IBusHomeTransView, View.OnClickListener {


    TextView busTransEndtv;
    private double mLat;
    private double mLog;
    private TextView startlocation, endlocation;

    public BusHomeTransFragment(BusHomeActivity busHomeActivity) {

    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.bus_trans_view;
    }


    @Override
    protected void initInjector() {

        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        EventBus.getDefault().register(this);

        busTransEndtv = dataBinding.busTransEndtv;

        RecyclerView transHomeRv = dataBinding.transHomeRv;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        transHomeRv.setLayoutManager(linearLayoutManager);
        BusTransAdapter busTransAdapter = new BusTransAdapter(this);
        transHomeRv.setAdapter(busTransAdapter);
        startlocation = dataBinding.locationTv;
        endlocation = dataBinding.busTransEndtv;
        endlocation.setText("输入终点");
        dataBinding.locationTv.setOnClickListener(this);
        dataBinding.busTransEndtv.setOnClickListener(this);
//        busTransEndtv.setOnClickListener(this);
        dataBinding.transHomeSearchbtn.setOnClickListener(this);
        dataBinding.realTimeMore.setOnClickListener(this);


        mLocationClient = new LocationClient(getActivity().getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(new BDAbstractLocationListener(){
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {

                startLat = bdLocation.getLatitude();
                startLog = bdLocation.getLongitude();
            }
        });


    }

    public LocationClient mLocationClient = null;



    @Override
    protected void updateViews() {

    }

    @Override
    public void getDataListSucc() {

    }

    @Override
    public void getDataListFail() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bus_trans_endtv) {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.TYPE, "transend");
            JumpUtils.jumptoNatvie(getActivity(), "BusSearchHistotyActivity", 0, bundle);
        } else if (id == R.id.trans_home_searchbtn) {
            Bundle bundle1 = new Bundle();
            String endTv = dataBinding.busTransEndtv.getText().toString().trim();
//                String endTv = busTransEndtv.getText().toString().trim();
            if (endTv.equals("输入终点")) {
                Toast.makeText(mActivity, "请输入终点", Toast.LENGTH_SHORT).show();
                return;
            }
            LatLng startLatlng = new LatLng(startLat, startLog);
            LatLng endLatlng = new LatLng(endLat, endLog);
            bundle1.putDouble("startlat", startLat);
            bundle1.putDouble("startlng", startLog);
            bundle1.putDouble("endlat", endLat);
            bundle1.putDouble("endlng", endLog);
            JumpUtils.jumptoNatvie(getActivity(), "BusChangeActivity", 0, bundle1);
//                startRoutePlanTransit(dataBinding.transHomeSearchbtn.getText().toString().trim());
        } else if (id == R.id.real_time_more) {
            String startStr = startlocation.getText().toString();
            String endStr = endlocation.getText().toString();
            startlocation.setText(endStr);
            endlocation.setText(startStr);
            double tempstartlat = startLat;
            double tempendlog = startLog;

            startLat = endLat;
            startLog = endLog;
            endLat = tempstartlat;
            endLog = tempendlog;
        } else if (id == R.id.location_tv) {
            Bundle bundles = new Bundle();
            bundles.putString(Constant.TYPE, "transstart");
            JumpUtils.jumptoNatvie(getActivity(), "BusSearchHistotyActivity", 0, bundles);
        }
    }

    /**
     * 启动百度地图公交路线规划
     *
     * @param end
     */
    public void startRoutePlanTransit(String end) {

        LatLng ptStart = new LatLng(startLat, startLog);
        LatLng ptEnd = new LatLng(endLat, endLog);
        // 构建 route搜索参数
        RouteParaOption para = new RouteParaOption()
//            .startPoint(pt_start)
                .startPoint(ptStart)
                .startName("新街口")
                .endName("联创大厦")
                .endPoint(ptEnd)
                .startName(startlocation.getText().toString())
                .endName(endlocation.getText().toString())
//            .endName("百度大厦")
                .busStrategyType(RouteParaOption.EBusStrategyType.bus_recommend_way);


        try {
            BaiduMapRoutePlan.openBaiduMapTransitRoute(para, getActivity());
        } catch (Exception e) {
            e.printStackTrace();
            showDialog();
        }

    }


    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("您尚未安装百度地图app或app版本过低，点击确认安装？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                OpenClientUtil.getLatestBaiduMapApp(getActivity());
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();

    }

    private double startLat;
    private double startLog;
    private double endLat;
    private double endLog;

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MesageEventBus eventBus) {
        if (eventBus.getStart() != null) {
            if (!TextUtils.isEmpty(eventBus.getStart())) {
                dataBinding.locationTv.setText(eventBus.getStart());
                startLat = eventBus.getLat();
                startLog = eventBus.getLog();
            }
            if (!TextUtils.isEmpty(eventBus.getEnd())) {
                dataBinding.busTransEndtv.setText(eventBus.getEnd());
                endLat = eventBus.getLat();
                endLog = eventBus.getLog();
            }


        }

//        if (event.getCode() == MessageEvent.TO_SETSTART) {
//            Toast.makeText(this, "首页设置起点", Toast.LENGTH_SHORT).show();
//        } else if (event.getCode() == MessageEvent.TO_SETEND) {
//            Toast.makeText(this, "首页设置终点", Toast.LENGTH_SHORT).show();
//        }
    }
}
